package com.dongdongshop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tbSpecificationController")
public class TbSpecificationController {

    @Reference
    private TbSpecificationServiceI tbSpecificationServiceI;

    private Logger logger= LoggerFactory.getLogger(TbBrandController.class);

    private Integer bei;

    //查询规格
    @RequestMapping("getFindAll")
    public String getFindAll(Model model){
        try {
            List<TbSpecification> tbSpecifications = tbSpecificationServiceI.getFindAll();
            model.addAttribute("tb",tbSpecifications);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbSpecifications);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
        }

        return "admin/specification";
    }

    //增加
    @RequestMapping("addTbSpecification")
    @ResponseBody
    public SpecificPrint addTbSpecification(TbSpecification tbSpecification, @RequestParam("result[]")String[] result){
        try {
            //定义一个list集合,用于存规格选项和排序
            List<TbSpecificationOption> specificationOptions = new ArrayList<>();
            //获取前台传来的规格选项json字符串数据 并进行遍历
            for (String str : result) {
                //将每个规格选项json字符串转换成规格选项对象  并存入集合
                TbSpecificationOption tbSpecificationOption= JSONObject.parseObject(str,TbSpecificationOption.class);
                specificationOptions.add(tbSpecificationOption);
            }
            specificationOptions.remove(0);
            tbSpecificationServiceI.addTbSpecification(tbSpecification,specificationOptions);
            return SpecificPrint.response(ResultEnum.SUCCESS);

        }catch (Exception e){
            logger.info("参数为：{}",tbSpecification);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //删除
    @RequestMapping("deleteTbSpecification")
    @ResponseBody
    public SpecificPrint deleteTbSpecification(String ids){
        try {
            String id =ids.replaceAll("'","");
            tbSpecificationServiceI.deleteTbSpecification(id);
            tbSpecificationServiceI.deleteTbSpecificationOption(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",id);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(id);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //根据id查询次表的值
    @RequestMapping("toUpdate")
    @ResponseBody
    public List<TbSpecificationOption> toUpdate(Integer id){
        List<TbSpecificationOption> list = tbSpecificationServiceI.selectTbSpecificationOptionById(id);
        bei = id ;
        return list;
    }
    //根据点击Id 查询主表的字段
    //根据id同部查询规格表
    @RequestMapping("tableMin")
    @ResponseBody
    public TbSpecification tableMin(){
        TbSpecification list= tbSpecificationServiceI.selectTbSpecificationById(bei);
        return list;
    }

    //确认修改
    @RequestMapping("updateTbSpecification")
    @ResponseBody
    public SpecificPrint updateTbSpecification(TbSpecification tbSpecification, @RequestParam("result[]")String[] result){
        try {
            //定义一个list集合,用于存规格选项和排序
            List<TbSpecificationOption> specificationOptions = new ArrayList<>();
            //获取前台传来的规格选项json字符串数据 并进行遍历
            for (String str : result) {
                //将每个规格选项json字符串转换成规格选项对象  并存入集合
                TbSpecificationOption tbSpecificationOption= JSONObject.parseObject(str,TbSpecificationOption.class);
                specificationOptions.add(tbSpecificationOption);
            }
            specificationOptions.remove(0);
            tbSpecification.setId(bei.longValue());
            tbSpecificationServiceI.updateTbSpecification(tbSpecification,specificationOptions);
            return SpecificPrint.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("sorting")
    @ResponseBody
    public TbSpecification sorTing(){
        TbSpecification list=tbSpecificationServiceI.selectTbSpecificationById(bei);
        return list;
    }
}
